﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using System.Collections.Generic;

namespace Galaxy.AutoCAD.Databases
{
    public static class LayerTool
    {
        #region 判断图层表中有没有该图层(定义为database的括展函数)
        //通过图层名字判断
        public static bool HasLayer(this Database db, string layerName)
        {
            bool result = false;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable layerTable = tran.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                result = layerTable.Has(layerName);
                tran.Commit();
            }
            return result;
        }
        #endregion
        #region 添加图层(定义为database的括展函数)
        //只设置图层名字添加图层
        public static ObjectId AddLayer(this Database db, string layerName)
        {
            ObjectId result = ObjectId.Null;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite);
                if (!lt.Has(layerName))
                {
                    LayerTableRecord ltr = new LayerTableRecord();
                    ltr.Name = layerName;
                    result = lt.Add(ltr);
                    tran.AddNewlyCreatedDBObject(ltr, true);
                }
                tran.Commit();
            }
            return result;
        }
        //设置图层名字和颜色添加图层
        public static ObjectId AddLayer(this Database db, string layerName, short colorIndex)
        {
            ObjectId result = ObjectId.Null;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite);
                if (!lt.Has(layerName))
                {
                    LayerTableRecord ltr = new LayerTableRecord();
                    ltr.Name = layerName;
                    ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, (short)(colorIndex % 256));
                    result = lt.Add(ltr);
                    tran.AddNewlyCreatedDBObject(ltr, true);
                }
                tran.Commit();
            }
            return result;
        }
        #endregion
        #region 设置图层颜色(定义为database的括展函数)
        //通过图层名字设置图层
        public static bool SetLayerColor(this Database db, string layerName, short colorIndex)
        {
            bool result = false;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (!lt.Has(layerName))
                {
                    result = false;
                }
                else
                {
                    ObjectId layerId = lt[layerName];
                    LayerTableRecord ltr = (LayerTableRecord)layerId.GetObject(OpenMode.ForWrite);
                    ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, (short)(colorIndex % 256));
                    result = true;
                }
                tran.Commit();
            }
            return result;
        }
        #endregion
        #region 设置当前图层（定义为database的括展函数）
        //通过图层名设置当前图层
        public static bool SetCurrentLayer(this Database db, string layerName)
        {
            bool result = false;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (!lt.Has(layerName))
                {
                    result = false;
                }
                else
                {
                    ObjectId layerId = lt[layerName];
                    if (db.Clayer == layerId)
                    {
                        result = true;
                    }
                    else
                    {
                        db.Clayer = layerId;
                        result = true;
                    }
                }
                tran.Commit();
            }
            return result;
        }
        #endregion
        #region 获得当前图形中所有的图层（定义为database的括展函数）
        public static List<LayerTableRecord> GetAllLayers(this Database db)
        {
            List<LayerTableRecord> ltrs = new List<LayerTableRecord>();
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForRead);
                foreach (ObjectId layerId in lt)
                {
                    LayerTableRecord ltr = (LayerTableRecord)layerId.GetObject(OpenMode.ForRead);
                    ltrs.Add(ltr);
                }
                tran.Commit();
            }
            return ltrs;
        }
        #endregion
        #region 删除指定名称的图层（定义为database的括展函数）
        public static bool DeleteLayer(this Database db, string layerName)
        {
            bool result = false;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (layerName == "0" || layerName == "Defpoints")
                {
                    result = false;
                }
                else
                {
                    if (!lt.Has(layerName))
                    {
                        result = false;
                    }
                    else
                    {
                        ObjectId layerId = lt[layerName];
                        if (db.Clayer == layerId)
                        {
                            result = false;
                        }
                        else
                        {
                            LayerTableRecord ltr = (LayerTableRecord)layerId.GetObject(OpenMode.ForRead);
                            lt.GenerateUsageData();
                            if (ltr.IsUsed)
                            {
                                result = false;
                            }
                            else
                            {
                                ltr.UpgradeOpen();
                                ltr.Erase(true);
                                result = true;
                            }
                        }
                    }
                }
                tran.Commit();
            }
            return result;
        }
        #endregion
    }
}
